package com.simulation;

class TNumericalFiber
{
     StringBuffer m_fiberType;
     /* the delta of radius*/
     int m_nr;
     /* the delta unit*/
     double m_deltaR;
     /* density of fiber*/
     double m_density; /* rho(f)*/
     /* the radius of fiber*/
     double m_radius; /* Rf*/
     /* diffusion coefficient of water vapor in the fiber*/
     double m_diffusionCoeWaterVaporInFiber; /* Df*/

     /* the mass ratio in the fabric*/
     double m_massRatio;
     /* the effecitive contact angel between the fiber and the water*/
     double m_contactAngle; /* phi*/
     /* the thermal conductivity*/
     double m_thermoConductivity;
     /* mean water vapor concentration in the fiber  */
     double m_cMeanWaterVaporInFiber[] = new double[All.NX]; /* /Cf,g[nx][2] */
     /* water vapor concentration in the fiber */
     TwoElementArray m_cWaterVaporInFiber[][] = new TwoElementArray[All.NX][All.NR]; /* Cf,g[nx][nr][2] */
     /* water vapor concent in the fibers of the fabric */
	 double m_wWaterVaporInFiber[] = new double[All.NX]; /* wf,g */

     /* mean liquid water concentration in the fiber */
     double m_cMeanLiquidWaterInFiber[] = new double[All.NX]; /* Cf,l */
     /* liquid water concentration in the fiber */
     TwoElementArray m_cLiquidWaterInFiber[][] = new TwoElementArray[All.NX][All.NR]; /* Cf,l[nx][nr][2] */
     /* liquid water concent in the fiber*/
     double m_wLiquidWaterInFiber[] = new double[All.NX]; /* wf,l */
     /* the heat coefficient of sorption of water vapor by fiber*/
     double m_heatCoeSorptionWaterVapor[] = new double[All.NX];
     /* the heat coefficient of sorption of liquid water by fiber*/
     double m_heatCoeSorptionLiquidWater[] = new double[All.NX];

     double m_differentialWaterVapor[] = new double[All.NX];
     double m_differentialLiquidWater[] = new double[All.NX];

     double m_volumetricHeatCapacity[] = new double[All.NX];
     double m_specificHeatCapacity[] = new double[All.NX];

     double m_regainY[] = new double[16];
     double m_regainX[] = new double[16];

     /* when adding the radiation source item*/
     /* the thermal emissivity of the fibers*/
     double m_emissivity; /* varepsilon(r) */

     TNumericalFiber(int nr,double massRatio) {
    	 m_nr=nr;
         m_massRatio=massRatio/100;
         
         for(int i=0; i<All.NX; i++)
        	 for(int j=0; j<All.NR;j++) {
        		 m_cWaterVaporInFiber[i][j] = new TwoElementArray();
        		 m_cLiquidWaterInFiber[i][j] = new TwoElementArray();
        	 }
     };
     //~TNumericalFiber();

     void InitialValue(FiberType fiber) {
    	 m_density=fiber.Density; /* rho(f)*/
         m_radius=fiber.Diameter*0.5; /* Rf*/
         m_diffusionCoeWaterVaporInFiber=fiber.VaporDiff;
         m_deltaR=m_radius/(m_nr-1); /*Df*/
         m_emissivity=fiber.Emissivity;
         m_contactAngle=fiber.LiqConAngle;
         //m_thermoConductivity=FIBERPROPERTY[fiberNumber][2]*0.239;

         /*m_regainY.Length=fiber.FiberRegainY.Length;
         m_regainX.Length=fiber.FiberRegainX.Length;
         for(int i=0;i<m_regainY.Length;i++)
         {
             m_regainY[i]=fiber.FiberRegainY[i];
             m_regainX[i]=fiber.FiberRegainX[i];
         }*/

    	 for(int i=0;i<16;i++)
    	 {
    		 m_regainY[i]=fiber.FiberRegainY[i];
    		 m_regainX[i]=fiber.FiberRegainX[i];
    	 }

         m_fiberType = new StringBuffer(fiber.FiberName);
     };
     
     //void GetLiquidWaterInFiber();
     //void GetWaterVaporInFiber();
     //TNumericalFiber & operator=(const TNumericalFiber &Fiber);
     /* for calculate the heat coefficient sorption of liquid water*/
     double a,b;
     int model;

     double GetHeatCoeSorptionWaterVapor(int i,double wc) {
    	 /* default value*/
         if(m_fiberType.equals("Cotton"))
         {
             m_heatCoeSorptionWaterVapor[i]=(1030.9*Math.exp(-22.39*wc)+2522.0)*0.239;
         }
         else if(m_fiberType.equals("Nylon"))
         {
             m_heatCoeSorptionWaterVapor[i]=2522.0*0.239;
         }
         else if(m_fiberType.equals("PolyPn"))
         {
             m_heatCoeSorptionWaterVapor[i]=2522.0*0.239;
         }
         else if(m_fiberType.equals("PolyTR"))
         {
             m_heatCoeSorptionWaterVapor[i]=2522.0*0.239;
         }
         else if(m_fiberType.equals("Wool"))
         {
             m_heatCoeSorptionWaterVapor[i]=(1602.5*Math.exp(-11.727*wc)+2522.0)*0.239;
         }
         else
         {
             m_heatCoeSorptionWaterVapor[i]=2522.0*0.239;
         }
         return m_heatCoeSorptionWaterVapor[i];
     };
     
     double GetHeatCoeSorptionLiquidWater(int i,double wc) {
    	 /* default value*/
         m_heatCoeSorptionLiquidWater[i]=2260.0*0.239;
         return  m_heatCoeSorptionLiquidWater[i]; 
     };
     
     void SolveMoistureSorption(int i, double rh, double dt) {
    	 double mu;
    		double[] A = new double[All.NR],
    				B = new double[All.NR],
    				C = new double[All.NR],
    				D = new double[All.NR],
    				X = new double[All.NR];
    	    double fiberRegain;

    	    mu=dt/(m_deltaR*m_deltaR);
    		fiberRegain = TMathPro.I_Interpolate(m_regainX, m_regainY,rh);

    	    A[0]=0;
    	    B[0]=1;
    	    C[0]=-1;
    	    D[0]=0;
    	    for(int j=1;j<m_nr-1;j++)
    	    {
    	        A[j]= mu*(1-1.0/(2*j))* m_diffusionCoeWaterVaporInFiber;
    	        B[j]= -(mu*(1+1.0/(2*j))*m_diffusionCoeWaterVaporInFiber+ mu*(1-1.0/(2*j))*m_diffusionCoeWaterVaporInFiber+1);
    	        C[j]= mu*(1+1.0/(2*j))* m_diffusionCoeWaterVaporInFiber;
    	        D[j]= -m_cWaterVaporInFiber[i][j].preNodeValue ;
    	    }
    	    A[m_nr-1]=0;
    	    B[m_nr-1]=1;
    	    C[m_nr-1]=0;
    	    D[m_nr-1]=m_density*fiberRegain;

    	    TMathPro.ChaseSolver(A,B,C,D,X);

    	    m_cMeanWaterVaporInFiber[i]=0;
    	    for(int j=0;j<m_nr;j++)
    	    {
    	        m_cWaterVaporInFiber[i][j].nextNodeValue =X[j];
    	    }

    	    for(int j=0;j<m_nr-1;j++)
    	    {
    	        m_cMeanWaterVaporInFiber[i]+=m_cWaterVaporInFiber[i][j].nextNodeValue/(m_nr-1);
    	    }

    	    m_wWaterVaporInFiber[i]= m_cMeanWaterVaporInFiber[i]/m_density;
    	     
    	    m_differentialWaterVapor[i]=0;

    	    for(int j=1;j<m_nr-1;j++)
    	    {
    	        m_differentialWaterVapor[i] +=m_diffusionCoeWaterVaporInFiber/(m_deltaR*m_deltaR)
    	                                     *((1.0+1.0/(2*j))*m_cWaterVaporInFiber[i][j+1].nextNodeValue
    	                                     - 2.0*m_cWaterVaporInFiber[i][j].nextNodeValue
    	                                     +(1.0-1.0/(2.0*j))*m_cWaterVaporInFiber[i][j-1].nextNodeValue)
    	                                     /(m_nr-2);


    	    } 
     };
     
     void SolveLiquidSorption(int i, double cLiquid,double dt) {
    	 double mu;
    	 double[] A = new double[All.NR],
 				B = new double[All.NR],
 				C = new double[All.NR],
 				D = new double[All.NR],
 				X = new double[All.NR];

    	    mu=dt/(m_deltaR*m_deltaR);

    	    A[0]=0;
    	    B[0]=1;
    	    C[0]=-1;
    	    D[0]=0;
    	    for(int j=1;j<m_nr-1;j++)
    	    {
    	        A[j]= mu*(1-1.0/(2*j))* m_diffusionCoeWaterVaporInFiber;
    	        B[j]= -(mu*(1+1.0/(2*j))*m_diffusionCoeWaterVaporInFiber+ mu*(1-1.0/(2*j))*m_diffusionCoeWaterVaporInFiber+1);
    	        C[j]= mu*(1+1.0/(2*j))* m_diffusionCoeWaterVaporInFiber;
    	        D[j]= -m_cLiquidWaterInFiber[i][j].preNodeValue;

    	    }
    	    A[m_nr-1]=0;
    	    B[m_nr-1]=1;
    	    C[m_nr-1]=0;
    	    D[m_nr-1]= cLiquid;
    	    
    	    TMathPro.ChaseSolver(A,B,C,D,X);

    	    m_cMeanLiquidWaterInFiber[i] =0;
    	    for(int j=0;j<m_nr;j++)
    	    {
    	        m_cLiquidWaterInFiber[i][j].nextNodeValue =X[j];

    	    }
    	    for(int j=0;j<m_nr-1;j++) //revise
    	    {
    	        m_cMeanLiquidWaterInFiber[i]+=m_cLiquidWaterInFiber[i][j].nextNodeValue/(m_nr-1);

    	    }

    	    m_wLiquidWaterInFiber[i]= m_cMeanLiquidWaterInFiber[i]/m_density;

    	    m_differentialLiquidWater[i]=0;
    	    for(int j=1;j<m_nr-1;j++)
    	    {
    	        m_differentialLiquidWater[i] += m_diffusionCoeWaterVaporInFiber/(m_deltaR*m_deltaR)
    	                                     *((1+1.0/(2*j))*m_cLiquidWaterInFiber[i][j+1].nextNodeValue
    	                                     - 2*m_cLiquidWaterInFiber[i][j].nextNodeValue
    	                                     +(1-1.0/(2.0*j))*m_cLiquidWaterInFiber[i][j-1].nextNodeValue)
    	                                     /(m_nr-2);
    	    }
     };
     
     double GetThermolConductivity(double wc) {
    	 if(m_fiberType.equals("Cotton"))
         {
             m_thermoConductivity= 0.239*(44.1-63.0*wc)*1e-5;
         }
         else if(m_fiberType.equals("Nylon"))
         {
             m_thermoConductivity=0.239*250*1e-5;
         }
         else if(m_fiberType.equals("PolyPn"))
         {
             m_thermoConductivity= 0.239*51.8*1e-5;
         }
         else if(m_fiberType.equals("PolyTR"))
         {
             m_thermoConductivity= 0.239*(40.4+23.0*wc)*1e-5;
         }
         else if(m_fiberType.equals("Wool"))
         {

             m_thermoConductivity=0.239*(38.493-0.72*wc+0.113*Math.pow(wc,2)-0.002*Math.pow(wc,3))*1e-5;
         }
         else
         {
              m_thermoConductivity= 0.239*51.8*1e-5;
         }
         return  m_thermoConductivity;
     };
};
